package com.johnson.news.activity;

import io.reactivex.functions.Consumer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.johnson.news.R;
import com.johnson.news.databinding.ActivityLoginBinding;
import com.johnson.news.event_msg.LoginMsg;
import com.johnson.news.model.bean.DataUserInfo;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.util.MyToast;

import org.greenrobot.eventbus.EventBus;

import static com.johnson.news.model.bean.BaseResponseData.response_ok;

public class LoginActivity extends BaseBindingActivity<ActivityLoginBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        dataBinding.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.find_pwd:
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("register", false);
                    startActivity(RegisterActivity.class, bundle);
                    break;
                case R.id.enter_btn:
                    login();
                    break;
                case R.id.reg_now:
                    startActivity(RegisterActivity.class, null);
                    finish();
                    break;
            }
        }
    };

    void login() {
        if(TextUtils.isEmpty(dataBinding.phoneEdit.getText())) {
            dataBinding.phoneEdit.setError(getString(R.string.login_null_phone));
            return;
        }
        if(!Utils.verifyPhone(dataBinding.phoneEdit.getText().toString())) {
            dataBinding.phoneEdit.setError(getString(R.string.login_invalid_phone));
            return;
        }
        if(TextUtils.isEmpty(dataBinding.pwdEdit.getText())) {
            dataBinding.pwdEdit.setError(getString(R.string.login_null_password));
            return;
        }

        RemoteDataSource.getRemoteDataSource()
                .login(dataBinding.phoneEdit.getText().toString(), dataBinding.pwdEdit.getText().toString(), Utils.channelId)
                .subscribe(new Consumer<DataUserInfo>() {
                    @Override
                    public void accept(DataUserInfo dataUserInfo) throws Exception {
                        if(dataUserInfo.status == response_ok) {
//                            Utils.saveUserToken(activityInstance, dataUserInfo.token);

                            //保存信息这一段代码，在原工程里只在注册时有。
                            NewsPreference pref = NewsPreference.getinstance(activityInstance);//保存信息这一段代码，在原工程里只在注册时有。
                            pref.setToken(dataUserInfo.token);
                            pref.setLoginName(dataBinding.phoneEdit.getText().toString());
                            pref.setLoginState(true);
                            //保存信息这一段代码，在原工程里只在注册时有。

                            EventBus.getDefault().post(new LoginMsg(dataBinding.phoneEdit.getText().toString()));

                            MyToast.showToast("登录成功", activityInstance);
                            finish();
                        } else {
                            MyToast.showToast(dataUserInfo.msg, activityInstance);
                        }
                    }
                });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_login;
    }
}
